/*
 * File     : WindowsX86_64Platform.java
 * Created  : 16 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.platform;

import com.googlecode.dni.internal.call.NativeCallFactory;
import com.googlecode.dni.internal.call.X64WindowsNativeCallFactory;
import com.googlecode.dni.internal.callback.ThunkManager.ThunkManagerDelegate;
import com.googlecode.dni.internal.callback.X86_64ThunkManagerDelegate;
import com.googlecode.dni.internal.library.SymbolNotFoundException;
import com.googlecode.dni.library.CallingConvention;
import com.googlecode.dni.platform.WindowsPlatformType;
import com.googlecode.dni.type.Pointer;
import com.googlecode.dni.type.structure.Padding;


/**
 * <p>
 *  Implementation of {@link Platform} for Windows x64.
 * </p>
 *
 * @author Matthew Wilson
 */
final class WindowsX64Platform extends AbstractWindowsPlatform
{

    /**
     * Creates a new instance.
     */
    WindowsX64Platform()
    {
        super( WindowsPlatformType.X64, Padding.FIXED_8 );
    }

    /** {@inheritDoc} */
    @Override
    public CallingConvention getActualCallingConvention( final CallingConvention callingConvention )
    {
        switch ( callingConvention )
        {
            // everything maps to the same convention on Windows x64, even with
            // the MS C compiler
            case STDCALL:
            case CDECL:
            case PLATFORM_DEFAULT:
                return CallingConvention.X64_WINDOWS;

            default:
                return null;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public NativeCallFactory createNativeCallFactory( final CallingConvention callingConvention )
    {
        switch ( callingConvention.getActual() )
        {
            case X64_WINDOWS:
                return new X64WindowsNativeCallFactory();

            default:
                throw new UnsupportedOperationException( callingConvention.toString() );
        }
    }


    /** {@inheritDoc} */
    @Override
    public ThunkManagerDelegate getThunkManagerDelegate( final CallingConvention callingConvention )
    {
        String name;
        switch ( callingConvention.getActual() )
        {
            case X64_WINDOWS:
                name = "dni_x64_windows_callback";
                break;

            default:
                throw new UnsupportedOperationException( callingConvention.toString() );
        }

        Pointer symbol;
        try
        {
            symbol = getDniDynamicLibrary().getSymbol( name );
        }
        catch ( SymbolNotFoundException exception )
        {
            throw (LinkageError) new LinkageError( "Missing callback delegate" ).initCause( exception );
        }
        return new X86_64ThunkManagerDelegate( symbol );
    }

}
